

--玩家添加怪物
ac.player.__index.enemy_count = 0
ac.player.__index.enemy_max = 0
ac.player.__index.enemy_level = 1

function ac.player.__index:create_enemy(name,point,sx)
    local owner = ac.player[self.id + 8]
    local point = point or self:getAtkPoint()
    local unit = owner:create_unit(name,point)
    jass.SetUnitAcquireRange(unit.handle, unit:get('攻击距离')+800)
    unit.owner_player = self
    unit['经验'] = 10
    if sx then
        unit:set_enemy_level(nil,sx)
    else
        unit:set_enemy_level(unit['默认等级'] or self.enemy_level * 6)
    end
    unit:issue_order('attack',ac.base_point)
    jass.SetUnitAcquireRange(unit.handle, 1000)
    unit:add_buff '动态测试数据'{}
    self.enemy_units[unit.handle] = unit
    unit:event '单位-死亡'(function()
        self.enemy_units[unit.handle] = nil
    end)
    self:notify('玩家-创建敌人',self,unit)
    return unit
end


function ac.player.__index:loop_enemy()
    if self.enemy_count>=self.enemy_max or self.hero==nil then
        return 
    end
    local name = table.get_by_weight(ac.enemy.weight) or '小怪模板'
    local unit = self:create_enemy(name)
    self.enemy_count = self.enemy_count + 1
    unit:add_type('进攻')
    unit:set('移动速度',120 + self:get_data('小怪额外移速',0))
    unit:event '单位-死亡'(function()
        self.enemy_count = self.enemy_count - 1
    end)
    if self.enemy_func then
        self.enemy_func(unit)
    end
end


function ac.player.__index:clear_enemy()
    for _,unit in pairs(self.enemy_units) do
        unit:remove()
    end
    self.enemy_units = {}
    self.enemy_count = 0
    if self.sb_timer then
        self.sb_timer:remove()
    end
end


function ac.player.__index:open_enemy()
    if self.sb_timer then
        self.sb_timer:remove()
    end
    local ex = 0
    self.sb_timer = ac.loop(500,function()
        local num = (6 + self:get_data('刷怪速度',0))*0.5 + ex
        local count = math.floor(num)
        ex = num - count
        for a=1,count do
            self:loop_enemy()
        end
    end)
end


ac.game:event '玩家-离开游戏'(function(_,player)
    local hero = self.owner
    hero:remove()
    player:clear_enemy()
end)


ac.game:event '游戏-开始出怪'(function()
    ac.each_player(function(player)
        player.enemy_units = {}
        player:open_enemy()
    end)
end)


ac.game:event '游戏-新的一波'(function(_,_,level)
    local ex = math.floor(level * 5/300) * 2
    ac.each_player(function(player)
        player.enemy_max = 10 + ex + player:get_data('额外刷新',0)
        player.enemy_level = level
        --player:send_msg(('当前怪物强度：%s'):format(level))
    end)
end)

ac.game:event '游戏-游戏结束'(function()
    ac.each_player(function(player)
        player:clear_enemy()
    end)
end)


ac.game:event '玩家-传送'(function(_,player,point)
    local unit = player.hero
    local point = point:get_point()
    unit:set_point(point,true)
    unit:issue_order('stop')
    player:set_camera(point)
    player:select_unit(unit)
end)


ac.sync:event '同步-回程'(function(_,player)
    local hero = player.hero
    if hero and hero:has_restriction('晕眩')==false then
        player:notify('玩家-传送',player,player.point1)
    end
end)

ac.hardware:event '键盘按下'(function(_,key)
    if key=='F2' then
        ac.sync('同步-回程')
    end
end)



